package com.henry.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.henry.pojo.Stu;
import com.henry.service.StuService;
import com.henry.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: old-service-call
 * @description:
 * @author: Henry.zhang
 * @create: 2022-08-15 11:24
 */
@RestController
@RequestMapping("stu")
public class StudentController {
    /*
    属性列表
    */
    @Autowired
    private StuService stuService;

    @Value("${server.port}")
    private String port;

    @GetMapping("foo")
    public R foo(@RequestParam("number") Integer number) throws Exception{
        if(number % 2 != 0){
            throw new Exception("出错了~");
        }
        return new R(100,"ok",number.toString());
    }

    @GetMapping("getAll")
    public R getAll(){

        List<Stu> list = stuService.list();

//        try {
//            Thread.sleep(8000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        return new R(200,port,list);
    }


    @GetMapping("getAllList")
    public List<Stu> getAllList(){

        List<Stu> list = stuService.list();

//        try {
//            Thread.sleep(8000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        return list;
    }

    @GetMapping("getOne")
    public R getOne(@RequestParam("stuId") Integer stuId) throws Exception {

        if(stuId > 3){
            throw new Exception("哈哈");
        }
        QueryWrapper<Stu> wrapper = new QueryWrapper();
        wrapper.eq("id",stuId);
        Stu stu = stuService.getOne(wrapper);

        return R.ok(stu);
    }

    @GetMapping("test")
    public R test(@RequestParam("stuId") Integer stuId) throws Exception {
        if(stuId < 0){
            throw new Exception();
        }

        return R.ok(stuId);
    }

}
